package com.lenovo.lcdm.dcm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.lenovo.lcdm.dcm.dto.DummyConfigurableModelCatagoryDto;
import com.lenovo.lcdm.dcm.dto.DummyConfigurableModelDto;
import com.lenovo.lcdm.dcm.dto.DummyConfigurableModelFeatureDto;
import com.lenovo.lcdm.dcm.entity.DummyConfigurableModelProductFeaturesDesc;
import com.lenovo.lcdm.dcm.mapper.DummyConfigurableModelProductFeaturesDescMapper;
import com.lenovo.lcdm.dcm.service.DummyConfigurableModelProductFeaturesDescService;
import com.lenovo.lcdm.dcm.vo.DummyConfigurableModelProductFeaturesDescVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sunby1
 * @description 针对表【plm_dummy_configurable_model_product_features_desc】的数据库操作Service实现
 * @createDate 2023-12-27 15:29:12
 */
@Service
public class DummyConfigurableModelProductFeaturesDescServiceImpl extends ServiceImpl<DummyConfigurableModelProductFeaturesDescMapper, DummyConfigurableModelProductFeaturesDesc>
        implements DummyConfigurableModelProductFeaturesDescService {

    @Override
    public List<DummyConfigurableModelProductFeaturesDescVo> getFeatures(Long id) {
        List<DummyConfigurableModelProductFeaturesDesc> dummyConfigurableModelProductFeaturesDescs = lambdaQuery().eq(DummyConfigurableModelProductFeaturesDesc::getCategoryId, id).orderByAsc(DummyConfigurableModelProductFeaturesDesc::getDescName).list();
        return BeanUtil.copyToList(dummyConfigurableModelProductFeaturesDescs, DummyConfigurableModelProductFeaturesDescVo.class);
    }

    @Override
    @Transactional
    public void delFeatureByCategoryIds(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            List<DummyConfigurableModelProductFeaturesDesc> dummyConfigurableModelProductFeaturesDescs = lambdaQuery().in(DummyConfigurableModelProductFeaturesDesc::getCategoryId, ids).list();
            List<Long> featureIds = dummyConfigurableModelProductFeaturesDescs.stream().map(DummyConfigurableModelProductFeaturesDesc::getId).toList();
            removeBatchByIds(featureIds);
        }
    }

    @Override
    @Transactional
    public void copyFeatureByCategoryId(Long oldDcmcId, Long newDcmcId) {
        List<DummyConfigurableModelProductFeaturesDesc> dummyConfigurableModelProductFeaturesDescs = lambdaQuery().eq(DummyConfigurableModelProductFeaturesDesc::getCategoryId, oldDcmcId).list();
        List<DummyConfigurableModelProductFeaturesDesc> list = dummyConfigurableModelProductFeaturesDescs.stream().map(feature -> {
            DummyConfigurableModelProductFeaturesDesc dummyConfigurableModelProductFeaturesDesc = new DummyConfigurableModelProductFeaturesDesc();
            BeanUtil.copyProperties(feature, dummyConfigurableModelProductFeaturesDesc, "id,categoryId");
            dummyConfigurableModelProductFeaturesDesc.setCategoryId(newDcmcId);
            return dummyConfigurableModelProductFeaturesDesc;
        }).toList();
        saveBatch(list);
    }

    @Override
    public List<DummyConfigurableModelFeatureDto> getFeatureDtos(Long id) {
        List<DummyConfigurableModelProductFeaturesDesc> dummyConfigurableModelProductFeaturesDescs = lambdaQuery().eq(DummyConfigurableModelProductFeaturesDesc::getCategoryId, id).list();
        return Lists.newArrayList(BeanUtil.copyToList(dummyConfigurableModelProductFeaturesDescs, DummyConfigurableModelFeatureDto.class));
    }

    @Override
    @Transactional
    public void saveDcms(List<DummyConfigurableModelCatagoryDto> dummyConfigurableModelCatagoryDtos) {
        List<DummyConfigurableModelProductFeaturesDesc> batchList = Lists.newArrayList();
        dummyConfigurableModelCatagoryDtos.forEach(dummyConfigurableModelCatagoryDto -> {
            List<DummyConfigurableModelProductFeaturesDesc> list = dummyConfigurableModelCatagoryDto.getDummyConfigurableModelFeatureDtos().stream().map(dummyConfigurableModelFeatureDto -> {
                DummyConfigurableModelProductFeaturesDesc dummyConfigurableModelProductFeaturesDesc = new DummyConfigurableModelProductFeaturesDesc();
                BeanUtil.copyProperties(dummyConfigurableModelFeatureDto, dummyConfigurableModelProductFeaturesDesc);
                dummyConfigurableModelProductFeaturesDesc.setId(null);
                dummyConfigurableModelProductFeaturesDesc.setCategoryId(dummyConfigurableModelCatagoryDto.getId());
                return dummyConfigurableModelProductFeaturesDesc;
            }).toList();
            batchList.addAll(list);
        });
        saveBatch(batchList);
    }

    @Override
    public void updateForCheckIn(DummyConfigurableModelDto dummyConfigurableModelDto) {
        List<DummyConfigurableModelCatagoryDto> dummyConfigurableModelCatagoryDtos = dummyConfigurableModelDto.getDummyConfigurableModelCatagoryDtos();

    }

    @Override
    public void updateDcms(DummyConfigurableModelFeatureDto targetFeatureDto) {
        DummyConfigurableModelProductFeaturesDesc desc = getById(targetFeatureDto.getId());
        BeanUtil.copyProperties(targetFeatureDto, desc);
        updateById(desc);
    }

    @Override
    public Map<Long, List<DummyConfigurableModelProductFeaturesDesc>> getFeaturesDescMap(List<Long> categoryIds) {
        if (CollectionUtil.isEmpty(categoryIds)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<DummyConfigurableModelProductFeaturesDesc> qw = new LambdaQueryWrapper<>();
        qw.in(DummyConfigurableModelProductFeaturesDesc::getCategoryId, categoryIds);
        List<DummyConfigurableModelProductFeaturesDesc> descList = list(qw);
        if(CollectionUtil.isEmpty(descList)){
            return new HashMap<>();
        }
        return descList.stream().collect(Collectors.groupingBy(DummyConfigurableModelProductFeaturesDesc::getCategoryId));
    }
}




